

import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import util.HttpUtils;

/**
 * 
 * <p>Title:QueryOriInterfaceUtil</>
 * <p>Description:</p>
 * @author chen
 * 2017年11月25日上午11:30:04
 */
public class QueryOriInterfaceUtil {
	private static final Logger LOG = Logger.getLogger(DoubleCacheUtil.class);
	private static final String LOGPRE = "交通银行话费油卡充值(QueryOriInterfaceUtil):";
	private static final String PROVINCE_HTTPARG_PREFIX = "province=";
	private static final String PATH = "/oil/query";
	/**
	 * @param urlAll
	 *            :请求接口
	 * @param httpArg
	 *            :参数
	 * @return 返回结果
	 */
	public static String request(String httpUrl, String httpArg,String apiKey) {
	    String body = null;

	    try {
	 	    String method = "GET";
	 	    String appcode = apiKey;
	 	    Map<String, String> headers = new HashMap<>(5);
	 	    //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
	 	    headers.put("Authorization", "APPCODE " + appcode);
	 	    Map<String, String> querys = new HashMap<>(5);
	 	    String province = httpArg.substring(PROVINCE_HTTPARG_PREFIX.length(),httpArg.length());
	 	    querys.put("province", province);
	 	    
	    	/**
	    	* 重要提示如下:
	    	* HttpUtils请从
	    	* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
	    	* 下载
	    	*
	    	* 相应的依赖请参照
	    	* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
	    	*/
	    	HttpResponse response = HttpUtils.doGet(httpUrl, PATH, method, headers, querys);
	    	//获取response的body
	    	body = EntityUtils.toString(response.getEntity());
	    	//{"status":"0","msg":"ok","result":{"province":"河南","oil89":"5.47","oil92":"6.79","oil95":"7.25","oil0":"6.38","oil90":"5.47","oil93":"6.79","oil97":"7.25","oil98":"7.90","updatetime":"2017-12-14 07:00:01"}}
	    } catch (Exception e) {
	    	LOG.error(LOGPRE + e.getMessage(),e);
	    }
	    return body;
	}
	
	public static void main(String[] args) {
		String httpUrl = "http://jisuoil.market.alicloudapi.com";
		String httpArg = "province=浙江";
		String apiKey = "eacb1a85b96141da87131b3c3d0d92f6";
	   System.out.println(request(httpUrl,httpArg,apiKey));
	}

}
